package com.graducationdesign.springboot.controller.Teacher;

import com.graducationdesign.springboot.dao.NewsInformationDao;
import com.graducationdesign.springboot.entities.Administrator.PersonnalInformationMoudle.Teacher;
import com.graducationdesign.springboot.entities.crawling.NewsInformation;
import com.graducationdesign.springboot.mapper.TeacherMapper;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @author Mr.zhneg
 * @create 2020-02-03-11:04
 */
@Controller
public class TeacherLoginController {

    org.slf4j.Logger logger=  LoggerFactory.getLogger(getClass());

    @Autowired
    TeacherMapper teacherMapper;
    @Autowired
    NewsInformationDao newsInformationDao;
    @PostMapping("/loginteacher")
    public  String login(Map<String,Object> map, String username, String password, HttpSession session,NewsInformation newsInformation){

        if(!StringUtils.isEmpty(username)&&!StringUtils.isEmpty(password)){
            logger.info("页面中的密码："+password+"页面中的登录名："+username);

            Teacher teacher= teacherMapper.getTeacherLogin(username);
            logger.info("用户名"+teacher);
            if(teacher==null){

                map.put("msg","用户名不存在");
//                return "redirect:/indext.html";
                return "Teacher/main/login";

            }

            logger.info("数据库中的密码："+teacher.getPassword()+"数据库中的登录名："+teacher.getLoginUserName());
            if (teacher.getLoginUserName().equals(username)&&teacher.getPassword().equals(password)) {
                logger.info("正确登陆名" + username);
                session.setAttribute("loginUser", username);
                logger.info("招聘信息查询。。。"+newsInformation);
                List<NewsInformation> newsInformations=newsInformationDao.findAll() ;
                map.put("newsInformation",newsInformations);
                //登陆成功
                //重定向可以redirect：可以重定向到任何一个请求中，地址栏改变
//                return "redirect:/mains.html";防止提交表单
                return "Teacher/main/index";
            }

        }

        //登录失败
        map.put("msg","用户名不存在或密码错误");
        return "Teacher/main/login";

    }

    @GetMapping("/logoutteacher")
    public String logOut(HttpSession session){

        //1：清空session中的用户信息
        session.removeAttribute("loginUser");

        //2：注销session
        session.invalidate();
        return "redirect:/indext.html";

    }
}
